package org.br.erp.project.projecttask.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.br.erp.base.utils.ERPUtils;
import org.br.erp.project.project.entity.Project;
import org.br.erp.project.project.service.IProjectService;
import org.br.erp.project.projecttask.entity.Projecttask;
import org.br.erp.project.projecttask.entity.Projecttaskitem;
import org.br.erp.project.projecttask.mapper.ProjecttaskitemMapper;
import org.br.erp.project.projecttask.mapper.ProjecttaskMapper;
import org.br.erp.project.projecttask.service.IProjecttaskService;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import java.io.Serializable;
import java.util.List;
import java.util.Collection;

/**
 * @Description: 项目任务
 * @Author: jeecg-boot
 * @Date:   2024-05-09
 * @Version: V1.0
 */
@Service
public class ProjecttaskServiceImpl extends ServiceImpl<ProjecttaskMapper, Projecttask> implements IProjecttaskService {

	@Autowired
	private ProjecttaskMapper projecttaskMapper;
	@Autowired
	private ProjecttaskitemMapper projecttaskitemMapper;

	@Autowired
	private IProjectService projectService;
	
	@Override
	@Transactional(rollbackFor = Exception.class)
	public void saveMain(Projecttask projecttask, List<Projecttaskitem> projecttaskitemList) {
		projecttaskMapper.insert(projecttask);
		if(projecttaskitemList!=null && projecttaskitemList.size()>0) {
			for(Projecttaskitem entity:projecttaskitemList) {
				//外键设置
				entity.setParentid(projecttask.getId());
				projecttaskitemMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void updateMain(Projecttask projecttask,List<Projecttaskitem> projecttaskitemList) {
		projecttaskMapper.updateById(projecttask);
		//1.先删除子表数据
		projecttaskitemMapper.deleteByMainId(projecttask.getId());
		
		//2.子表数据重新插入
		if(projecttaskitemList!=null && projecttaskitemList.size()>0) {
			for(Projecttaskitem entity:projecttaskitemList) {
				//外键设置
				entity.setParentid(projecttask.getId());
				projecttaskitemMapper.insert(entity);
			}
		}

		//更新进度的同时更新当前项目的进度
		QueryWrapper<Projecttask> queryWrapper=new QueryWrapper<>();
		queryWrapper.eq("projectid",projecttask.getProjectid());
		List<Projecttask> projecttasks = projecttaskMapper.selectList(queryWrapper);

		double v = projecttasks.stream().mapToDouble(item ->
		{
			if (ERPUtils.isNoneOrEmpty(item.getProgress())) {
				return 0;
			} else {
				return Double.valueOf(item.getProgress());
			}
		}).average().orElse(0);

		Project project = projectService.getById(projecttask.getProjectid());
		if(project!=null){
			int projectprogress= (int) v;
			project.setProjectprogress(String.valueOf(projectprogress));
			projectService.updateById(project);
		}
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void delMain(String id) {
		projecttaskitemMapper.deleteByMainId(id);
		projecttaskMapper.deleteById(id);
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void delBatchMain(Collection<? extends Serializable> idList) {
		for(Serializable id:idList) {
			projecttaskitemMapper.deleteByMainId(id.toString());
			projecttaskMapper.deleteById(id);
		}
	}
	
}
